﻿using FarPoint.Web.Spread;
using System;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ControlExplorer.samples.PreviewRow
{
  public partial class Overview : SpreadDemoPage
  {
    protected void Page_Load(object sender, EventArgs e)
    {
      if (IsPostBack) return;
      InitSpread(this.FpSpread1.Sheets[0]);
      InitSpread(this.FpSpread1.Sheets[1]);
    }

    private void InitSpread(SheetView sheet)
    {
        this.FpSpread1.UseClipboard = false;
        sheet.DataSource = CreateDataSet();

      sheet.Columns[0].Width = 30;
      sheet.Columns[0].HorizontalAlign = HorizontalAlign.Center;
      sheet.Columns[0].VerticalAlign = VerticalAlign.Middle;
      sheet.Columns[1].Width = 60;
      sheet.Columns[1].HorizontalAlign = HorizontalAlign.Center;
      sheet.Columns[1].VerticalAlign = VerticalAlign.Middle;
      sheet.Columns[2].Width = 90;
      sheet.Columns[3].Width = 90;
      sheet.Columns[4].Width = 200;
      sheet.Columns[5].Width = 200;

      sheet.DefaultRowHeight = 75;

      int cc = sheet.Columns.Count;
      int rc = sheet.RowCount;

      for (int r = 0; r < rc; r++)
      {
        string id = sheet.Cells[r, 0].Text;
        FixedImageCellType img = new FixedImageCellType("~/samples/RowTemplateLayout/Image.ashx?id=" + id);
        sheet.Cells[r, 1].CellType = img;
      }

      sheet.Columns[cc- 1].Visible = false;
      sheet.PreviewRowVisible = true;
      sheet.PreviewRowColumnIndex = cc - 1;
      sheet.PreviewRowStyle.BackColor = Color.LightGray;
      sheet.PreviewRowStyle.ForeColor = Color.Blue;
      sheet.PreviewRowStyle.Font.Italic = true;
      sheet.PreviewRowStyle.Border.BorderColorBottom = Color.Black;
      sheet.PreviewRowStyle.Border.BorderSizeBottom = 4;
      sheet.PreviewRowStyle.Border.BorderStyleBottom = BorderStyle.Double;
    }

    private DataSet CreateDataSet()
    {
      string constr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\Northwind.mdb;Persist Security Info=True";
      string sqlstr = @"SELECT EmployeeID as ID, 
                                '' as 头像, 
                                FirstName as 姓, LastName as 名, 
                                Title as 标题, Address as 地址, City as 城市, 
                                PostalCode as 邮编, Region as 区域, 
                                Country as 国家, HireDate as 入职日期, 
                                HomePhone as 家庭电话, Extension as 分机号, Notes as 注释
                        FROM employees";
      DataSet employees = new DataSet();
      using (OleDbConnection myconn = new OleDbConnection(constr))
      {
        OleDbDataAdapter sqlAdapter1 = new OleDbDataAdapter(sqlstr, myconn);
        sqlAdapter1.Fill(employees, "employees");
      }
      return employees;
    }
   
  }

  class FixedImageCellType : ImageCellType
  {
    public FixedImageCellType() : base() { }
    public FixedImageCellType(string imageUrl) : base(imageUrl) { 
    }

    public override Control PaintCell(string id, TableCell parent, Appearance style, Inset margin, object value, bool upperLevel)
    {
      Control control = base.PaintCell(id, parent, style, margin, value, upperLevel);
      WebControl img = GetImageControl(control);
      img.Style.Add(HtmlTextWriterStyle.Height, "65px");
      return control;
    }

    WebControl GetImageControl(Control control) {
      if (control is System.Web.UI.WebControls.Image) return control as WebControl;
      foreach (Control child in control.Controls) {
        WebControl found = GetImageControl(child);
        if (found != null) return found;
      }
      return null;
    }
  }
}